package ru.kop4en.diplom;


import java.awt.Canvas;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface;
import uk.co.caprica.vlcj.player.headless.HeadlessMediaPlayer;

public class VLCJ{

  private JFrame frame;
  private JPanel contentPane;
  private Canvas canvas;
  private MediaPlayerFactory factory;
  private HeadlessMediaPlayer mediaPlayer;
  private CanvasVideoSurface videoSurface;
  
  public static void main(final String[] args) {
//    if(args.length != 1) {
//      System.out.println("Specify a capture device MRL, for example \"v4l2:///dev/video0\" or \"dshow://\"");
//      System.exit(1);
//    }
    
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new VLCJ().start("dshow://");
      }
    });
  }

	public VLCJ()
	{

	}
  
  private void start(String mrl) {
  
		MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(mrl);
		HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
		
 
	  
    File dir = new File(System.getProperty("user.home"), "Videos");
    dir.mkdirs();
    
    DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
    String fileName =  dir.getAbsolutePath() + "/Capture-" + df.format(new Date()) + ".mpg";

    // Tweak the options depending on your encoding requirements and audio 
    // capture device (ALSA is not likely to work on Windows of course)
    //":sout=#transcode{vcodec=h264,venc=x264{cfr=16},scale=1,acodec=mp4a,ab=160,channels=2,samplerate=44100}:file{dst=" + fileName + "}"
    String[] options = 
    {
    	":sout=#transcode{vcodec=h264,venc=x264{cfr=16},scale=1,acodec=mp4a,ab=160,channels=2,samplerate=44100}:standard{access=file,dst=" + fileName + "}"    	
    };
    
    mediaPlayer.playMedia(mrl, options);
    
       
    //":sout=#transcode{vcodec=h264,venc=x264{cfr=16},scale=1,acodec=mp4a,ab=160,channels=2,samplerate=44100}:standard{access=file,dst=" + fileName + "}"
    
    // :sout=#transcode{vcodec=h264,venc=x264{cfr=16},scale=1,acodec=mp4a,ab=160,channels=2,samplerate=44100}:standard{access=file,dst=" + fileName + "}
    
    /*
    
    C:\>"Program Files\VideoLAN\VLC\vlc.exe" dshow:// --sout=#transcode{vcodec=mp2v,vb=1024,fps=30,width=320,acodec=mp2a,ab=128,s
    	lace,audio-sync}:duplicate{dst=standard{access=file,mux=ps,dst="D:\Output.mpg"},dst=display{noaudio}} --extraintf=logger -vvv
    	
    	*/

	//"--sout #transcode{vcodec=mpeg4,acodec=mpga,vb=800,ab=128}:standard{access=file,dst=" + fileName + "}"
    //:sout=#standard{mux=ts,access=file,dst=/home/movies/capture.avi}
    
    mediaPlayer.playMedia(mrl, options);
  }
  
  private static String formatHttpStream(String serverAddress, int serverPort) {
	    StringBuilder sb = new StringBuilder(60);
	    sb.append(":sout=#duplicate{dst=std{access=http,mux=ts,");
	    sb.append("dst=");
	    sb.append(serverAddress);
	    sb.append(':');
	    sb.append(serverPort);
	    sb.append("}}");
	    return sb.toString();
	  }
}